package welsir.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import welsir.entity.dos.ArticleClass;
import welsir.entity.vo.ArticleClassPageVO;
import welsir.entity.vo.ClassNameVO;
import welsir.entity.vo.ResultMessage;
import welsir.mapper.ArticleClassMapper;
import welsir.security.annotations.Authentication;
import welsir.security.enums.UserEnums;
import welsir.service.IArticleClassService;
import welsir.util.ResultUtil;

import javax.annotation.Resource;
import javax.management.ValueExp;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Genius
 * @date 2023/03/07 21:03
 * 分类管理接口
 * @admin 需要管理员权限
 **/
@Api(value = "文章分类Controller",tags = {"文章分类接口"})
@RestController
@RequestMapping("/articleClass")
public class ArticleClassController {

    @Autowired
    private IArticleClassService articleClassService;

    @Resource
    private ArticleClassMapper articleClassMapper;
    /**
     *
     * 新建文章分类
     * @param articleClass
     * @return
     */
    @ApiOperation(value = "新建文章分类")
    @Authentication(role = UserEnums.ADMIN)
    @PostMapping("/newArticleClass")
    public ResultMessage<Integer> newArticleClass(@RequestBody ArticleClass articleClass){
        QueryWrapper<ArticleClass> wrapper = new QueryWrapper<>();
        wrapper.eq("name",articleClass.getName());
        ArticleClass insertArticle = articleClassService.getOne(wrapper);
        if(insertArticle != null){
            return ResultUtil.error(143,"该分类已经存在");
        }
        articleClassMapper.insert(articleClass);
        return ResultUtil.success();

    }

    /**
     * 更新文章
     * @param articleClass
     * @return
     */
    @ApiOperation(value = "修改文章分类")
    @Authentication(role = UserEnums.ADMIN)
    @PostMapping("/updateArticleClass")
    public ResultMessage<Integer> updateArticleClass(@RequestBody ArticleClass articleClass){
        QueryWrapper<ArticleClass> wrapper = new QueryWrapper<>();
        wrapper.eq("name",articleClass.getName());
        ArticleClass updateArticle = articleClassService.getOne(wrapper);
        if(updateArticle == null){
            return ResultUtil.error(143,"该分类不存在");
        }
        articleClassMapper.update(articleClass,wrapper);
        return ResultUtil.success();
    }

    /**
     * 根据分页获取文章类型
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "分页查询文章类型")
    @Authentication(role = UserEnums.ADMIN)
    @PostMapping("/allArticleClass/{page}/{limit}")
    public ResultMessage<ArticleClassPageVO> allClass(
            @PathVariable("page") Integer page,@PathVariable("limit") Integer limit
    ){
        return ResultUtil.data(this.articleClassService.GetList(page,limit));
    }

    /**
     * 删除文章Id
     * @param id
     * @return
     */
    @ApiOperation(value = "根据uid删除文章分类")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/DeleteArticleClass/{id}")
    public ResultMessage<Integer> DeleteArticleClass(
            @PathVariable("id") Integer id
    ){
        this.articleClassMapper.deleteById(id);
        return ResultUtil.success();
    }


    /**
     * 获取所有文章分类名
     * @return
     */
    @ApiOperation(value = "获取所有文章分类")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/getAllClassName")
    public ResultMessage<List<ClassNameVO>> getAllClassName(){
        List<ClassNameVO> result = new ArrayList<>();

        QueryWrapper<ArticleClass> wrapper= new QueryWrapper<ArticleClass>();
        wrapper.select("name");
        List<ArticleClass> articleClasses = articleClassMapper.selectList(wrapper);
        articleClasses.stream().forEach(articleClass -> {
            ClassNameVO classNameVO = new ClassNameVO();
            BeanUtils.copyProperties(articleClass,classNameVO);
            result.add(classNameVO);
        });
        return ResultUtil.data(result);
    }


    /**
     * 根据id获取文章分类名
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取文章类名")
    @Authentication(role = UserEnums.ADMIN)
    @GetMapping("/getClassNameById/{id}")
    public  ResultMessage<String> getClassNameById(
            @PathVariable("id") Integer id
    ){
        QueryWrapper<ArticleClass> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);

        ArticleClass articleClass = articleClassMapper.selectOne(wrapper);
        return ResultUtil.data(articleClass.getName());
    }

}
